package com.neusoft.nep.SystemAdminstrator.Controller;

import com.neusoft.nep.SystemAdminstrator.Common.AlertUtills;
import com.neusoft.nep.SystemAdminstrator.Common.FileUtills;
import com.neusoft.nep.SystemAdminstrator.entity.SystemUser;
import com.neusoft.nep.SystemAdminstrator.entity.User;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class loginController {

    private List<String> assignedOfficerNames = new ArrayList<>();
    public static String currentLoginID; // 静态变量保存当前登录用户的ID
    public void initialize() throws IOException {

        File file = new File("AssignofficerNames.txt");
        if (!file.exists()){
            file.createNewFile();
            FileUtills.writeListObject("AssignofficerNames.txt",assignedOfficerNames);
        }

        File SystemUserFile = new File("SystemUser.txt");

        Map SystemUserMap = FileUtills.readMapObject("SystemUser.txt");

        SystemUser systemUser01 = new SystemUser("尚书","111");
        SystemUser systemUser02 = new SystemUser("梓豪","222");
        SystemUser systemUser03 = new SystemUser("睿博","333");

        SystemUserMap.put("尚书",systemUser01);
        SystemUserMap.put("梓豪",systemUser02);
        SystemUserMap.put("睿博",systemUser03);

        FileUtills.writeMapObject("SystemUser.txt",SystemUserMap);

    }

    @FXML
    private TextField txt_loginID;
    @FXML
    private TextField txt_loginpassword;
    @FXML
    Button btn_login = new Button();
    @FXML
    public void logintoindex(ActionEvent event) throws IOException {
        AlertUtills alertUtills = new AlertUtills();
        String ID = txt_loginID.getText();
        String password = txt_loginpassword.getText();
        try{
            File file = new File("SystemUser.txt");
            if (!file.exists()){
                System.out.println("文件不存在，请检查项目");
                System.exit(0);
            }
            Map<String,SystemUser> SystemUserMap = FileUtills.readMapObject("SystemUser.txt");
            if (SystemUserMap.containsKey(ID)){
                SystemUser systemUser = SystemUserMap.get(ID);
                currentLoginID = ID;
                if (systemUser.getLoginpassword().equals(password)){
                    alertUtills.Showdialog("登录",null,"登陆成功！");
                    Stage stage = (Stage) btn_login.getScene().getWindow();
                    FXMLLoader fxmlLoader = new FXMLLoader();
                    URL url = getClass().getResource("../View/index.fxml");
                    fxmlLoader.setLocation(url);
                    Parent root = fxmlLoader.load();
                    Scene scene = btn_login.getScene();
                    scene.setRoot(root);
                    stage.setScene(scene);
                    stage.show();
                }else {
                    alertUtills.ShowErrorDialog("错误",null,"密码不正确");
                    System.out.println("密码不正确");
                }

            }else {
                System.out.println("ID不存在");
                alertUtills.ShowErrorDialog("错误",null,"ID不存在！");
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}
